几个不同的块内部有不同数量的空白,据此,DFS求每个黑色图形内的连通块个数就行。先处理外部的白块。然后扫描,遇到没遍历过的黑块dfs,遇到没遍历过的白块就计数再dfs。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142/*
*Abbott's Revenge, ACM/ICPC World Finals 2011,UVa 1103
*/
/*0表示初始的白色 -1表示初始的黑色 -2表示不属于任何符号的白色 1表示遍历过的黑色 2表示遍历过的白色*/
using namespace std;
int h, w, w0;
char cmap[210][60];
int map[210][210];
int charval[128];
int cunt[10] = { 0 };
FILE *out;
void change(int *p, char c)
{
p[0] = -charval[c] / 8;
p[1] = -(charval[c] / 4) % 2;
p[2] = -(charval[c] / 2) % 2;
p[3] = -charval[c]% 2;
}
void expend(int x, int y, int val)
{
map[x][y] = val;
expend_(1, 0);
expend_(-1, 0);
expend_(0, -1);
expend_(0, 1);
}
void prtmap()
{
//cout << endl;
//int i, j;
//for (i = 1; i <= h; i++)
//{
// for (j = 1; j <= w; j++)
// //cout << setw(2) << map[i][j];
// if (map[i][j] == -1)
// cout << "*";
// else if (map[i][j] == -2)
// cout << "-";
// else
// cout << ".";
// cout << endl;
//}
//cout << endl << endl;
}
int expendBlack(int x, int y)
{
int cnt=0;
map[x][y] = 1;
expendWhite_(0, 1);
expendWhite_(0, -1);
expendWhite_(1, 0);
expendWhite_(-1, 0);
expendBlack_(0, 1);
expendBlack_(0, -1);
expendBlack_(1, 0);
expendBlack_(-1, 0);
return cnt;
}
void major()
{
int i, j;
for (i = 1; i <= h; i++)
for (j = 1; j <= w0; j++)
change(&map[i][j * 4 - 3], cmap[i][j]);
//prtmap();
for (i = 1; i <= h;i++)
if (map[i][1] == 0)
expend(i, 1, -2);
for (i = 1; i <= h; i++)
if (map[i][w] == 0)
expend(i, w, -2);
for (i = 1; i <= w; i++)
if (map[1][i] == 0)
expend(1, i, -2);
for (i = 1; i <= w; i++)
if (map[h][i] == 0)
expend(h, i, -2);
//prtmap();
for (i = 1; i <= h; i++)
for (j = 1; j <= w; j++)
if (map[i][j] == -1)
{
int cnt = expendBlack(i, j);
cunt[cnt]++;
//prtmap();
}
prtans(1, 'A');
prtans(5, 'D');
prtans(3, 'J');
prtans(2, 'K');
prtans(4, 'S');
prtans(0, 'W');
printf("\n");
}
void ini1()
{
int i;
for (i = '0'; i <= '9'; i++)
charval[i] = i - '0';
for (i = 'a'; i <= 'f'; i++)
charval[i] = i - 'a' + 10;
}
void ini()
{
memset(cunt, 0, sizeof(cunt));
}
int main()
{
out = fopen("d:/2.txt", "w");
int i,t=0;
ini1();
while (1)
{
t++;
scanf("%d%d", &h, &w0);
if (h == 0 && w0 == 0)
break;
w = w0 * 4;
for (i = 1; i <= h; i++)
scanf("%s", &cmap[i][1]);
printf("Case %d: ",t);
ini();
major();
}
return 0;
}